﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sausage.Web.DataModel;
using Sausage.Web.Models.Report;
using Sausage.Web.Models;
using System.Web.Script.Serialization;

namespace Sausage.Web.Report
{
    /// <summary>
    /// 报告数据辅助类。
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2012/03/22 10:41    创建
    /// </history>
    public class ReportHelper
    {
        /// <summary>
        /// 获得养护汇总数据。
        /// </summary>
        /// <returns>养护汇总数据。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/22 10:42    创建
        /// </history>
        public IList<MaintainSummaryModel> GetMaintainSummary()
        {
            //获得查询条件
            MaintainSummaryCondition condition = HttpContext.Current.Items["Filter"] as MaintainSummaryCondition;

            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //创建查询
                IQueryable<MaintainSummary> query = context.MaintainSummary.AsQueryable();

                //判断是否指定了数据类型
                if (condition.DataTypes != null && condition.DataTypes.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.DataTypes.Contains(item.DataType));
                }

                //判断是否指定了开始日期
                if (condition.ApplicationDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate >= condition.ApplicationDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.ApplicationDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate <= condition.ApplicationDate_H.Value);
                }

                //判断是否指定了组织结构
                if (condition.Organizations != null && condition.Organizations.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Organizations.Contains(item.GroupID));
                }

                //创建查询
                var result = from item in query
                             select new MaintainSummaryModel
                             {
                                 GroupCode = item.GroupCode,
                                 SubSectionCode = item.SubSectionCode,
                                 Category = item.Category,
                                 CategoryName = item.CategoryName,
                                 DataType = item.DataType,
                                 DataTypeName = item.DataTypeName,
                                 GroupID = item.GroupID,
                                 GroupName = item.GroupName,
                                 Level = 0,
                                 MaintainCode = item.MaintainCode,
                                 MaintainPrice = item.MaintainPrice,
                                 MaintainQuantity = item.MaintainQuantity,
                                 MaintainSubTotal = item.MaintainAmount,
                                 MaintainUnit = item.MaintainUnit,
                                 Price = item.Price,
                                 Quantity = item.Quantity,
                                 QuantityID = item.QuantityID,
                                 SectionID = Guid.Empty,
                                 SectionName = "总段",
                                 SubCategory = item.SubCategory,
                                 SubCategoryName = item.SubCategoryName,
                                 SubSectionID = item.SubSectionID,
                                 SubSectionName = item.SubSectionName,
                                 SubTotal = item.Amount,
                                 Unit = item.Unit
                             };

                return result.ToArray();
            }
        }

        /// <summary>
        /// 获得养护汇总数据。
        /// </summary>
        /// <returns>养护汇总数据。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/22 10:42    创建
        /// </history>
        public IList<MaintainSummaryModel> GetMaintainMonthSummary()
        {
            //获得查询条件
            MaintainSummaryCondition condition = HttpContext.Current.Items["Filter"] as MaintainSummaryCondition;

            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //创建查询
                IQueryable<MaintainSummary> query = context.MaintainSummary.AsQueryable();

                //判断是否指定了数据类型
                if (condition.DataTypes != null && condition.DataTypes.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.DataTypes.Contains(item.DataType));
                }

                //判断是否指定了开始日期
                if (condition.ApplicationDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate >= condition.ApplicationDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.ApplicationDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate <= condition.ApplicationDate_H.Value);
                }

                //判断是否指定了组织结构
                if (condition.Organizations != null && condition.Organizations.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Organizations.Contains(item.GroupID));
                }

                //创建查询
                var result = from item in query
                             select new MaintainSummaryModel
                             {
                                 GroupCode = item.GroupCode,
                                 SubSectionCode = item.SubSectionCode,
                                 Category = item.Category,
                                 CategoryName = item.CategoryName,
                                 DataType = item.DataType,
                                 DataTypeName = item.DataTypeName,
                                 GroupID = item.GroupID,
                                 GroupName = item.GroupName,
                                 Level = 0,
                                 MaintainCode = item.MaintainCode,
                                 MaintainPrice = item.MaintainPrice,
                                 MaintainQuantity = item.MaintainQuantity,
                                 MaintainSubTotal = item.MaintainAmount,
                                 MaintainUnit = item.MaintainUnit,
                                 Price = item.Price,
                                 Quantity = item.Quantity,
                                 QuantityID = item.QuantityID,
                                 SectionID = Guid.Empty,
                                 SectionName = "总段",
                                 SubCategory = item.SubCategory,
                                 SubCategoryName = item.SubCategoryName,
                                 SubSectionID = item.SubSectionID,
                                 SubSectionName = item.SubSectionName,
                                 SubTotal = item.Amount,
                                 Unit = item.Unit
                             };

                return result.ToArray();
            }
        }

        /// <summary>
        /// 获得工程汇总数据。
        /// </summary>
        /// <returns>工程汇总数据。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/22 10:42    创建
        /// </history>
        public IList<ProjectSummaryModel> GetProjectSummary()
        {
            //获得查询条件
            ProjectSummaryCondition condition = HttpContext.Current.Items["Filter"] as ProjectSummaryCondition;

            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //创建查询
                IQueryable<ProjectSummary> query = context.ProjectSummary.AsQueryable();

                //判断是否指定了工程名称
                if (!string.IsNullOrEmpty(condition.ProjectName))
                {
                    //使用工程名称过滤
                    query = query.Where(item => item.ProjectName.Contains(condition.ProjectName));
                }

                //判断是否指定了工程编号
                if (!string.IsNullOrEmpty(condition.ProjectCode))
                {
                    //使用工程编号过滤
                    query = query.Where(item => item.ProjectCode.Contains(condition.ProjectCode));
                }

                //判断是否使用了类型过滤
                if (condition.Categories != null && condition.Categories.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Categories.Contains(item.ProjectCategory));
                }

                //判断是否指定了数据类型
                if (condition.DataTypes != null && condition.DataTypes.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.DataTypes.Contains(item.DataType));
                }

                //判断是否指定了开始日期
                if (condition.ApplicationDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate >= condition.ApplicationDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.ApplicationDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate <= condition.ApplicationDate_H.Value);
                }

                //判断是否指定了开始日期
                if (condition.StartDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate >= condition.StartDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.StartDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate <= condition.StartDate_H.Value);
                }

                //判断是否指定了组织结构
                if (condition.Organizations != null && condition.Organizations.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Organizations.Contains(item.OrganizationID));
                }

                //返回结果
                var result = from item in query
                             select new ProjectSummaryModel
                             {
                                 Category = item.Category,
                                 CategoryName = item.CategoryName,
                                 SubCategory = item.SubCategory,
                                 SubCategoryName = item.SubCategoryName,
                                 Construction = item.ConstructionName,
                                 ConstructionID = item.ConstructionID,
                                 ConstructionPrice = item.ProjectPrice,
                                 ConstructionQuantity = item.ProjectQuantity,
                                 ConstructionSubTotal = item.ProjectAmount,
                                 ConstructionUnit = item.ProjectUnit,
                                 DataType = item.DataType,
                                 DataTypeName = item.DataTypeName,
                                 Price = item.Price,
                                 ProjectCode = item.ProjectCode,
                                 ProjectID = item.ProjectID,
                                 ProjectName = item.ProjectName,
                                 Quantity = item.Quantity,
                                 SubTotal = item.Amount,
                                 Unit = item.Unit
                             };

                //返回结果
                return result.ToArray();
            }
        }

        /// <summary>
        /// 获得工程机械汇总数据。
        /// </summary>
        /// <returns>工程机械汇总数据。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/22 10:42    创建
        /// </history>
        public IList<ProjectMachineSummaryModel> GetProjectMachineSummary()
        {
            //获得查询条件
            ProjectSummaryCondition condition = HttpContext.Current.Items["Filter"] as ProjectSummaryCondition;

            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //创建查询
                IQueryable<ProjectMachineSummary> query = context.ProjectMachineSummary.AsQueryable();

                //判断是否指定了工程名称
                if (!string.IsNullOrEmpty(condition.ProjectName))
                {
                    //使用工程名称过滤
                    query = query.Where(item => item.ProjectName.Contains(condition.ProjectName));
                }

                //判断是否指定了工程编号
                if (!string.IsNullOrEmpty(condition.ProjectCode))
                {
                    //使用工程编号过滤
                    query = query.Where(item => item.ProjectCode.Contains(condition.ProjectCode));
                }

                //判断是否使用了类型过滤
                if (condition.Categories != null && condition.Categories.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Categories.Contains(item.ProjectCategory));
                }

                //判断是否指定了开始日期
                if (condition.ApplicationDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate >= condition.ApplicationDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.ApplicationDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate <= condition.ApplicationDate_H.Value);
                }

                //判断是否指定了开始日期
                if (condition.StartDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate >= condition.StartDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.StartDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate <= condition.StartDate_H.Value);
                }

                //判断是否指定了组织结构
                if (condition.Organizations != null && condition.Organizations.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Organizations.Contains(item.GroupID));
                }

                //返回结果
                var result = from item in query
                             select new ProjectMachineSummaryModel
                             {
                                 Category = item.SubSectionID,
                                 CategoryCode = item.SubSectionCode,
                                 CategoryName = item.SubSectionName,
                                 MachineAmount = item.MachineAmount,
                                 MaintenanceAmount = item.MaintenanceAmount,
                                 OilAmount = item.OilAmount,
                                 SubTotal = item.MachineAmount + item.MaintenanceAmount + item.OilAmount,
                                 ProjectCode = item.ProjectCode,
                                 ProjectID = item.ProjectID,
                                 ProjectName = item.ProjectName,
                                 SubCategory = item.GroupID,
                                 SubCategoryCode = item.GroupCode,
                                 SubCategoryName = item.GroupName
                             };

                //返回结果
                return result.ToArray();
            }
        }

        /// <summary>
        /// 获得工程人工汇总数据。
        /// </summary>
        /// <returns>工程汇总数据。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/22 10:42    创建
        /// </history>
        public IList<ProjectCategorySummaryModel> GetProjectLabourSummary()
        {
            //获得查询条件
            ProjectSummaryCondition condition = HttpContext.Current.Items["Filter"] as ProjectSummaryCondition;

            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //创建查询
                IQueryable<ProjectLabourSummary> query = context.ProjectLabourSummary.AsQueryable();

                //判断是否指定了工程名称
                if (!string.IsNullOrEmpty(condition.ProjectName))
                {
                    //使用工程名称过滤
                    query = query.Where(item => item.ProjectName.Contains(condition.ProjectName));
                }

                //判断是否指定了工程编号
                if (!string.IsNullOrEmpty(condition.ProjectCode))
                {
                    //使用工程编号过滤
                    query = query.Where(item => item.ProjectCode.Contains(condition.ProjectCode));
                }

                //判断是否使用了类型过滤
                if (condition.Categories != null && condition.Categories.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Categories.Contains(item.ProjectCategory));
                }

                //判断是否指定了数据类型
                if (condition.DataTypes != null && condition.DataTypes.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.DataTypes.Contains(item.DataType));
                }

                //判断是否指定了开始日期
                if (condition.ApplicationDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate >= condition.ApplicationDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.ApplicationDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate <= condition.ApplicationDate_H.Value);
                }

                //判断是否指定了开始日期
                if (condition.StartDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate >= condition.StartDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.StartDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate <= condition.StartDate_H.Value);
                }

                //判断是否指定了组织结构
                if (condition.Organizations != null && condition.Organizations.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Organizations.Contains(item.GroupID));
                }

                //返回结果
                var result = from item in query
                             select new ProjectCategorySummaryModel
                             {
                                 Category = item.Category,
                                 CategoryName = item.CategoryName,
                                 GroupCode = item.GroupCode,
                                 GroupID = item.GroupID,
                                 GroupName = item.GroupName,
                                 Price = item.Price,
                                 Quantity = item.Quantity,
                                 SubCategory = item.SubCategory,
                                 SubCategoryName = item.SubCategoryName,
                                 SubSectionCode = item.SubSectionCode,
                                 SubSectionID = item.SubSectionID,
                                 SubSectionName = item.SubSectionName,
                                 SubTotal = item.Amount,
                                 Unit = item.Unit,
                                 ProjectCode = item.ProjectCode,
                                 ProjectID = item.ProjectID,
                                 ProjectName = item.ProjectName
                             };

                //返回结果
                return result.ToArray();
            }
        }

        /// <summary>
        /// 获得工程材料汇总数据。
        /// </summary>
        /// <returns>工程材料汇总数据。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/22 10:42    创建
        /// </history>
        public IList<ProjectCategorySummaryModel> GetProjectMaterialSummary()
        {
            //获得查询条件
            ProjectSummaryCondition condition = HttpContext.Current.Items["Filter"] as ProjectSummaryCondition;

            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //创建查询
                IQueryable<ProjectMaterialSummary> query = context.ProjectMaterialSummary.AsQueryable();

                //判断是否指定了工程名称
                if (!string.IsNullOrEmpty(condition.ProjectName))
                {
                    //使用工程名称过滤
                    query = query.Where(item => item.ProjectName.Contains(condition.ProjectName));
                }

                //判断是否指定了工程编号
                if (!string.IsNullOrEmpty(condition.ProjectCode))
                {
                    //使用工程编号过滤
                    query = query.Where(item => item.ProjectCode.Contains(condition.ProjectCode));
                }

                //判断是否使用了类型过滤
                if (condition.Categories != null && condition.Categories.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Categories.Contains(item.ProjectCategory));
                }

                //判断是否指定了数据类型
                if (condition.DataTypes != null && condition.DataTypes.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.DataTypes.Contains(item.DataType));
                }

                //判断是否指定了开始日期
                if (condition.ApplicationDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate >= condition.ApplicationDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.ApplicationDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate <= condition.ApplicationDate_H.Value);
                }

                //判断是否指定了开始日期
                if (condition.StartDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate >= condition.StartDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.StartDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate <= condition.StartDate_H.Value);
                }

                //判断是否指定了组织结构
                if (condition.Organizations != null && condition.Organizations.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Organizations.Contains(item.GroupID));
                }

                //返回结果
                var result = from item in query
                             select new ProjectCategorySummaryModel
                             {
                                 Category = item.Category,
                                 CategoryName = item.CategoryName,
                                 GroupCode = item.GroupCode,
                                 GroupID = item.GroupID,
                                 GroupName = item.GroupName,
                                 Price = item.Price,
                                 Quantity = item.Quantity,
                                 SubCategory = item.SubCategory,
                                 SubCategoryName = item.SubCategoryName,
                                 SubSectionCode = item.SubSectionCode,
                                 SubSectionID = item.SubSectionID,
                                 SubSectionName = item.SubSectionName,
                                 SubTotal = item.Amount,
                                 Unit = item.Unit,
                                 ProjectCode = item.ProjectCode,
                                 ProjectID = item.ProjectID,
                                 ProjectName = item.ProjectName
                             };

                //返回结果
                return result.ToArray();
            }
        }

        /// <summary>
        /// 获得工程进度表数据。
        /// </summary>
        /// <returns>工程进度表数据。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/22 10:42    创建
        /// </history>
        public IList<ProjectProgressModel> GetProjectProgress()
        {

            //获得查询条件
            ProjectProgressCondition condition = HttpContext.Current.Items["Filter"] as ProjectProgressCondition;

            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //创建查询
                IQueryable<ProjectProgress> query = context.ProjectProgress.Where(item => item.State == 300);

                //判断是否指定了工程名称
                if (!string.IsNullOrEmpty(condition.ProjectName))
                {
                    //使用工程名称过滤
                    query = query.Where(item => item.ProjectName.Contains(condition.ProjectName));
                }

                //判断是否指定了工程编号
                if (!string.IsNullOrEmpty(condition.ProjectCode))
                {
                    //使用工程编号过滤
                    query = query.Where(item => item.ProjectCode.Contains(condition.ProjectCode));
                }

                //判断是否使用了类型过滤
                if (condition.Categories != null && condition.Categories.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Categories.Contains(item.CategoryID));
                }

                //判断是否指定了开始日期
                if (condition.StartDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate >= condition.StartDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.StartDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate <= condition.StartDate_H.Value);
                }

                //判断是否指定了组织结构
                if (condition.Organizations != null && condition.Organizations.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Organizations.Contains(item.OrganizationID));
                }

                //查询结果
                var result = from item in query
                             select new ProjectProgressModel
                             {
                                 Construction = item.ConstructionName,
                                 ConstructionID = item.ConstructionID,
                                 LeftQuantity = item.PlanQuantity > item.Quantity ? item.PlanQuantity - item.Quantity : 0,
                                 PlanQuantity = item.PlanQuantity,
                                 Unit = item.Unit,
                                 Progress = item.PlanQuantity > 0 ? item.Quantity / item.PlanQuantity : -1,
                                 ProjectCode = item.ProjectCode,
                                 ProjectID = item.ProjectID,
                                 ProjectName = item.ProjectName,
                                 Quantity = item.Quantity
                             };

                //返回结果
                return result.ToArray();
            }
        }

        /// <summary>
        /// 获得工程花费数据。
        /// </summary>
        /// <returns>工程花费数据。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/22 10:42    创建
        /// </history>
        public IList<ProjectCostModel> GetProjectCost()
        {
            //获得查询条件
            ProjectCostCondition condition = HttpContext.Current.Items["Filter"] as ProjectCostCondition;

            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //创建查询
                IQueryable<ProjectCost> query = context.ProjectCost.Where(item => item.State == 300);

                //判断是否指定了工程名称
                if (!string.IsNullOrEmpty(condition.ProjectName))
                {
                    //使用工程名称过滤
                    query = query.Where(item => item.ProjectName.Contains(condition.ProjectName));
                }

                //判断是否指定了工程编号
                if (!string.IsNullOrEmpty(condition.ProjectCode))
                {
                    //使用工程编号过滤
                    query = query.Where(item => item.ProjectCode.Contains(condition.ProjectCode));
                }

                //判断是否使用了类型过滤
                if (condition.Categories != null && condition.Categories.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Categories.Contains(item.CategoryID));
                }

                //判断是否指定了开始日期
                if (condition.StartDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate >= condition.StartDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.StartDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate <= condition.StartDate_H.Value);
                }

                //判断是否指定了组织结构
                if (condition.Organizations != null && condition.Organizations.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Organizations.Contains(item.OrganizationID));
                }

                //查询结果
                var result = from item in query
                             select new ProjectCostModel
                             {
                                 Construction = item.ConstructionName,
                                 ConstructionID = item.ConstructionID,
                                 PlanAmount = item.PlanAmount / new decimal(10000),
                                 Amount = item.Amount / new decimal(10000),
                                 PlanTotalAmount = item.PlanTotalAmount / new decimal(10000),
                                 TotalAmount = item.TotalAmount / new decimal(10000),
                                 LeftBudget = item.PlanAmount > item.Amount ? item.PlanAmount - item.Amount : 0,
                                 FundingProgress = item.PlanAmount > 0 ? item.Amount / item.PlanAmount : 1,
                                 ProjectCode = item.ProjectCode,
                                 ProjectID = item.ProjectID,
                                 ProjectName = item.ProjectName,
                                 StartDate = item.StartDate,
                                 CompletionDate = item.CompletionDate,
                                 FirstConstructionDate = item.FirstConstructionDate,
                                 LatestConstructionDate = item.LatestConstructionDate
                             };

                //返回结果
                return result.ToArray();
            }
        }

        /// <summary>
        /// 获得工程价格预警数据。
        /// </summary>
        /// <returns>工程价格预警数据。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2012/03/22 10:42    创建
        /// </history>
        public IList<ProjectPriceWarningModel> GetProjectPriceWarning()
        {
            //读取警告阈值
            string value = System.Configuration.ConfigurationManager.AppSettings["ProjectWarningThreshold"];

            //用于保存阈值
            decimal threshold = 0;

            //解析阈值
            if (string.IsNullOrEmpty(value) || !Decimal.TryParse(value, out threshold))
            {
                //设置为默认值
                threshold = new Decimal(1.2);
            }

            //获得查询条件
            ProjectPriceCondition condition = HttpContext.Current.Items["Filter"] as ProjectPriceCondition;

            //查询数据
            using (SausageEntities context = new SausageEntities())
            {
                //创建查询
                IQueryable<ProjectPriceWarning> query = context.ProjectPriceWarning.Where(item => item.State == 300);

                //判断是否指定了工程名称
                if (!string.IsNullOrEmpty(condition.ProjectName))
                {
                    //使用工程名称过滤
                    query = query.Where(item => item.ProjectName.Contains(condition.ProjectName));
                }

                //判断是否指定了工程编号
                if (!string.IsNullOrEmpty(condition.ProjectCode))
                {
                    //使用工程编号过滤
                    query = query.Where(item => item.ProjectCode.Contains(condition.ProjectCode));
                }

                //判断是否使用了类型过滤
                if (condition.Categories != null && condition.Categories.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Categories.Contains(item.CategoryID));
                }

                //判断是否指定了开始日期
                if (condition.ApplicationDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate >= condition.ApplicationDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.ApplicationDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.ApplicationDate <= condition.ApplicationDate_H.Value);
                }

                //判断是否指定了开始日期
                if (condition.StartDate_L.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate >= condition.StartDate_L.Value);
                }

                //判断是否指定了结束日期
                if (condition.StartDate_H.HasValue)
                {
                    //过滤
                    query = query.Where(item => item.StartDate <= condition.StartDate_H.Value);
                }

                //判断是否指定了组织结构
                if (condition.Organizations != null && condition.Organizations.Length > 0)
                {
                    //过滤
                    query = query.Where(item => condition.Organizations.Contains(item.OrganizationID));
                }

                //返回结果
                var result = from item in query
                             select new ProjectPriceWarningModel
                             {
                                 ProjectID = item.ProjectID,
                                 ProjectName = item.ProjectName,
                                 ProjectCode = item.ProjectCode,
                                 LabourPrice = item.LabourAmount,
                                 MachinePrice = item.MachineAmount,
                                 MaterialPrice = item.MaterialAmount,
                                 PriceDate = item.ApplicationDate,
                                 QuantityPrice = item.Amount,
                                 IsWarning = item.LabourAmount + item.MaterialAmount + item.MachineAmount > item.Amount * threshold,
                             };

                //返回结果
                return result.ToArray();
            }
        }
    }
}